//有个内含单词的超大文本文件，给定任意两个不同的单词，找出在这个文件中这两个单词的最短距离(相隔单词数)。如果寻找过程在这个文件中会重复多次，而每次寻找的单词
//不同，你能对此优化吗? 
//
// 示例： 
//
// 
//输入：words = ["I","am","a","student","from","a","university","in","a","city"], 
//word1 = "a", word2 = "student"
//输出：1 
//
// 提示： 
//
// 
// words.length <= 100000 
// 
//
// Related Topics 数组 字符串 👍 110 👎 0


package LeetCode.editor.cn;

import java.util.stream.IntStream;

/**
 * @author ldltd
 * @date 2025-09-16 22:48:20
 * @description 面试题 17.11.单词距离
 */
public class FindClosestLcci{
	 public static void main(String[] args) {
	 	 //测试代码
	 	 FindClosestLcci fun=new FindClosestLcci();
	 	 Solution solution = fun.new Solution();

	 }
	 
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int findClosest(String[] words, String word1, String word2) {
		return IntStream.range(0, words.length)
				.collect(() -> new int[]{-1, -1, Integer.MAX_VALUE},
						(state, i) -> {
							if (words[i].equals(word1)) {
								state[0] = i;
							} else if (words[i].equals(word2)) {
								state[1] = i;
							}

							if (state[0] != -1 && state[1] != -1) {
								state[2] = Math.min(state[2], Math.abs(state[0] - state[1]));
							}
						},
						(state1, state2) -> {
							// 合并逻辑，但在这个问题中并行处理没有意义
							state1[2] = Math.min(state1[2], state2[2]);
						})[2];
	}
}
//leetcode submit region end(Prohibit modification and deletion)

}
